Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],

    houseInfo: '', // 要维修的房屋
    repairItemName: '', // 选择的维修项目

    // --------------------------
    houseId: '',
    repairItemId: '',
    mobile: '',
    appointment: '', // 选择的预约日期
    description: '',
    attachment: [], // 上传图片的数组
  },

  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    // 判断是否是修改报修信息
    if (id) {
      // 1. 修改标题
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      // 2. 获取报修详情
      this.getRepairDetail(id)
    }
  },
  /**
   * 获取报修详情
   * @param {*} id
   */
  async getRepairDetail(id) {
    if (!id) return
    // 请求数据接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 检测接口调用结果
    if (code !== 10000) return wx.utils.toast('获取报修信息失败!')
    // 渲染报修信息
    this.setData({ ...repairDetail })
  },

  /**
   * 打开选择房屋弹层
   */
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  /**
   * 关闭选择房屋弹层
   */
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  /**
   * 选择要维修的房屋
   */
  selectHouse(e) {
    const { name: houseInfo, id: houseId } = e.detail
    this.setData({
      houseInfo,
      houseId,
    })
  },

  /**
   * 打开维修项目弹层
   */
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  /**
   * 关闭维修项目弹层
   */
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  /**
   * 选择维修项目
   */
  selectRepairItem(e) {
    console.log(e.detail)
    const { name: repairItemName, id: repairItemId } = e.detail
    this.setData({
      repairItemName,
      repairItemId,
    })
  },

  /**
   * 打开预约日期弹层
   */
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  /**
   * 关闭预约日期弹层
   */
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  /**
   * 选择预约日期
   */
  selectRepairDate(e) {
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(e.detail),
    })
  },

  /**
   * 获取审核通过的房屋
   */
  async getHouseList() {
    const { code, data: houseList } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList,
    })
  },

  /**
   * 获取维修项目
   */
  async getRepairItem() {
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      repairItem,
    })
  },

  /**
   * 上传图片
   */
  async onAfterRead(e) {
    // 1. 调用上传图片接口
    const { code, data } = await wx.http.upload('/upload', {
      name: 'file', // 文件的 key
      filePath: e.detail.file.tempFilePath, // 文件的 value
      formData: {}, // HTTP 请求中其他额外的 form data
    })
    if (code !== 10000) return wx.utils.toast('上传图片失败~')
    // 更新数据
    // console.log(data)
    this.data.attachment.push(data)
    this.setData({
      attachment: this.data.attachment,
    })
  },

  /**
   * 删除图片
   */
  onDelete(e) {
    const { index } = e.detail
    this.data.attachment.splice(index, 1)
    this.setData({
      attachment: this.data.attachment,
    })
  },

  async onSubmit() {
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    // 1. 校验
    if (!wx.utils.verifyHouse(houseId)) return
    if (!wx.utils.verifyRepair(repairItemId)) return
    if (!wx.utils.verifyMobile(mobile)) return
    if (!wx.utils.verifyDate(appointment)) return
    if (!wx.utils.verifyDescription(description)) return

    // 2. 调用接口
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    if (code !== 10000) return wx.utils.toast('填写报修信息失败')

    // 3. 返回
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
